<?php

namespace Apps\AppApi\Method\Search;


use PhalconPlus\Api\Common\ResultStatus;
use Apps\Common\Models\BeautyParlorService as BeautyParlorServiceModel;
use Apps\Common\Models\BeautyParlor as BeautyParlorModel;
use Apps\Common\Libs\PicUrl;
use Apps\Common\Models\SysOpenCity;
use Apps\Common\Models\SysAreaZipDistrictPhonecode;

class BeautyParlorService extends BaseSearch
{
    public function run()
    {
    	$page = intval($this->getDataItem('page',1));
        $keyword = $this->getDataItem('keyword');
        $city_code = trim($this->getDataItem('city_code'));
        
        if ($city_code==null ||$city_code==''){
            return $this->errorLog(ResultStatus::POST_BODY_PARAM_ERROR,"城市编码为空");exit;
        }
        $is_open = SysOpenCity::findFirst(
            [
                'columns'=>"city_id",
                'conditions' => "city_code = $city_code AND city_state = 1",
            ]);
        
        if (!$is_open->city_id>0){
            return $this->errorLog(ResultStatus::CITY_NOT_OPEN,"城市未开通");exit;
        }
        //根据城市编码 获取 城市区
        $city = SysAreaZipDistrictPhonecode::query()
        ->columns(['K_ID'])
        ->where("PHONE_CODE = $city_code")
        ->execute()->toArray();
        foreach ($city as $k){
            $citys[] = $k['K_ID'];
        }
        $citys = implode(',', $citys);
        
        $count = $this->getConfig()->limit;
        if(empty($keyword)) {
        	$offset = ($page-1)*$count;
        	$list = BeautyParlorServiceModel::query()
        	->columns([
        	    'service_id id',
        	    'service_cover cover',
        	    'service_name name',
        	    'service_price price',
        	    'service_unit unit',
        	    'service_description description',
        	    'b.bp_id',
        	    'b.bp_name'
        	])
        	->leftJoin('Apps\Common\Models\BeautyParlor', 'Apps\Common\Models\BeautyParlorService.beauty_parlor_id = b.bp_id','b')
        	->where("Apps\Common\Models\BeautyParlorService.service_state = 1 AND Apps\Common\Models\BeautyParlorService.service_is_sell = 1 and b.bp_state = 1 AND b.bp_district in ($citys)")
        	->limit($count,$offset)
        	->execute()
        	->toArray();
        	
        	foreach ($list as $k=>$v){
            	    $unit = $v['unit'];
            	    unset($v['unit']);
            	    $list[$k]['price_show'] = '';
            	    if($v['price'] > 0) {
            	        $list[$k]['price_show'] = $v['price'] . '元' . ($unit ? '/' . $unit : '');
            	    }
        			$list[$k]['cover']=PicUrl::BeautyParlorCover($list[$k]['cover'], $this->getDi());
        	}  
        	$this->setResult($list); 
        	
        }else{
        	/*多关键字搜索*/
        	$keywordarr=explode(" ",trim($keyword));
        	foreach ($keywordarr as $r){
        		$wherearr[]="Apps\Common\Models\BeautyParlorService.service_name like '%".$r."%'";
        		$wherearr[]="Apps\Common\Models\BeautyParlorService.service_intro like '%".$r."%'";
        	}
        	$where = implode(" or ",$wherearr);
        	////////////////////////////////////////////////////////////////
	        /* $limit = $this->getConfig()->limit;
	        $offset = ($page-1) * $limit;
	        $data = $this->searchBPS($keyword, $limit, $offset);
	        $this->setResult($data); */
        	$offset = ($page-1)*$count;
        	$list = BeautyParlorServiceModel::query()
        	->columns([
        	    'service_id id',
        	    'service_cover cover',
        	    'service_name name',
        	    'service_price price',
        	    'service_unit unit',
        	    'service_description description',
        	    'b.bp_id',
        	    'b.bp_name'
        	])
        	->leftJoin('Apps\Common\Models\BeautyParlor', 'Apps\Common\Models\BeautyParlorService.beauty_parlor_id = b.bp_id','b')
        	->where("($where) and Apps\Common\Models\BeautyParlorService.service_state = 1 AND Apps\Common\Models\BeautyParlorService.service_is_sell = 1 and b.bp_state = 1 AND b.bp_district in ($citys)")
        	->limit($count,$offset)
        	->execute()
        	->toArray();
            foreach ($list as $k=>$v){
                $unit = $v['unit'];
                unset($list[$k]['unit']);
                $list[$k]['price_show'] = '';
                if($v['price'] > 0) {
                    $list[$k]['price_show'] = $v['price'] . '元' . ($unit ? '/' . $unit : '');
                }
        		$list[$k]['cover']=PicUrl::BeautyParlorCover($list[$k]['cover'], $this->getDi());
        	}
        	$this->setResult($list);
        	
        }
    }
}